// 版权归 瞄你个汪 所有。使用本代码应遵守相关法律法规和Apache 2.0开源许可要求。
package top.geeke.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.geeke.config.cache.RedisCache;
import top.geeke.constants.AppCachePool;
import top.geeke.constants.AppConfigPool;
import top.geeke.controller.dto.LoginConfigOutput;
import top.geeke.entity.SysConfig;
import top.geeke.mapper.SysConfigMapper;
import top.geeke.service.ISysConfigService;
import top.geeke.toolkit.ClassTool;

import javax.annotation.Resource;

/**
 * <p>
 * 系统配置表 服务实现类
 * </p>
 * @author 瞄你个汪
 * @since 2024-01-01 12:00:00
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {
    @Resource
    private RedisCache redisCache;

    @Override
    public <T> T getValue(Class<T> clazz, String key) {
        return redisCache.getAndSet(String.format(AppCachePool.SYSTEM_CONFIG, key), () -> {
            String value = baseMapper.getValue(key);
            if (value == null) return null;
            return ClassTool.tryParse(clazz, value);
        }, 60, clazz);
    }

    /**
     * 获取登录配置
     */
    @Override
    public LoginConfigOutput getLoginConfig() {
        return redisCache.getAndSet(AppCachePool.SYSTEM_LOGIN_CONFIG, () -> {
            Boolean captchaEnable = getValue(Boolean.class, AppConfigPool.Sys_Captcha);
            Boolean secondVerEnable = getValue(Boolean.class, AppConfigPool.Sys_Second_Ver);
            Boolean rotateVerifyEnabled = getValue(Boolean.class, AppConfigPool.Sys_Rotate_Verify);
            Boolean domainLoginEnabled = getValue(Boolean.class, AppConfigPool.Sys_Domain_Login);
            return new LoginConfigOutput(rotateVerifyEnabled, domainLoginEnabled, secondVerEnable, captchaEnable);
        }, 60, LoginConfigOutput.class);
    }
}
